//
//  FacilityDetailInfo.m
//  YOGProj
//
//  Created by liyun on 14-6-27.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "FacilityDetailInfo.h"
#import "MacroDefinition.h"
#import "HWLabelTextSize.h"
#import "CompetitionList.h"
#import "FacilityLocation.h"
#import "SendHttpOperation.h"
#import "ShowVenuesViewController.h"
#import "CustomSelect.h"


static int length = 260;
static float font = 14.0f;

@interface FacilityDetailInfo ()<UITableViewDataSource,UITableViewDelegate,competitionListProtocol,CustomSelectProtocol>
{
    int height1;
    int height2;
    int count2;
    int height3;
    int height4;
    
    NSString *str;
    NSString *str2;
    NSArray          *tempSelectArr;
    CustomSelect     *custom;
    IBOutlet UILabel *map;
    IBOutlet UILabel *schedule;
    IBOutlet UILabel *d3;
    IBOutlet UIImageView *d3Image;
    IBOutlet UIButton *d3Button;
    IBOutlet UITableView *facilityDetailtable;
    IBOutlet UIImageView *facilityDetailImage;
}


@end

@implementation FacilityDetailInfo

@synthesize detailInfoDic;
@synthesize faclityCodeDic;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!detailInfoDic)
            detailInfoDic = [[NSDictionary alloc] init];
        
        if(!faclityCodeDic)
            faclityCodeDic = [[NSDictionary alloc] init];
    }
    return self;
}

- (void)navigationShowLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:NAVIGATION_BACK_IMAGE] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(navigationShowLeft) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItemleft=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barItemleft;
    
    UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(navigationShowLeft)];
    recognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:recognizerRight];
    
    map.text = NSLocalizedString(@"facility_map", @"");
    schedule.text = NSLocalizedString(@"facility_event", @"");
    if([faclityCodeDic allKeys].count != 0)
        d3.text = NSLocalizedString(@"facility_d3", @"");
    else
    {
        d3.hidden = YES;
        d3Image.hidden = YES;
        d3Button.enabled = NO;
    }
    
    facilityDetailImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[detailInfoDic objectForKey:@"venuesCode"]]];
    facilityDetailtable.separatorStyle = UITableViewCellSelectionStyleNone;
    
    height1 = [HWLabelTextSize getSuitSizeWithString:[detailInfoDic objectForKey:@"description"] fontSize:font bold:NO sizeOfX:length].height;
    if(height1 <100)
        height1 = 100;
    
    count2 = (int)[detailInfoDic[@"seats"] count];
    if(count2 != 0)
    {
        if(count2 <= 5)
            height2 = 100;
        else
            height2 = count2 *20;
    }
    
    height3 = [HWLabelTextSize getSuitSizeWithString:[detailInfoDic objectForKey:@"address"] fontSize:font bold:NO sizeOfX:length].height;
    if(height3 <100)
        height3 = 100;
    
    height4 = 100;
    
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:
            return height1;
            break;
        case 1:
            return height2;
            break;
        case 2:
            return height3;
            break;
        case 3:
            return height4;
            break;
        default:
            return height1;
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews)
    {
        [subview removeFromSuperview];
    }
    
    
    UIImageView *line = [[UIImageView alloc] init];
    line.backgroundColor = TABLE_LINE;
    [cell.contentView addSubview:line];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = HEIGHT_FONT(font);
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:label];
    
    switch (indexPath.row)
    {
        case 0:
        {
            label.text = NSLocalizedString(@"brief", @"");
            label.frame = CGRectMake(0, 0, 50, height1);
            label.backgroundColor = [UIColor colorWithRed:11/255.0 green:151/255.0 blue:212/255.0 alpha:1.0];
            line.frame = CGRectMake(1, height1, 320, 1);
            
            UILabel *description = [[UILabel alloc] init];
            description.text = [detailInfoDic objectForKey:@"description"];
            description.font = NORMAL_FONT(font);
            description.numberOfLines = 0;
            description.backgroundColor = [UIColor clearColor];
            description.frame = CGRectMake(55, 0, length, height1);
            [cell.contentView addSubview:description];
        }
            break;
        case 1:
        {
            label.text = NSLocalizedString(@"event_table", @"");
            label.frame = CGRectMake(0, 0, 50, height2);
            label.backgroundColor = [UIColor colorWithRed:157/255.0 green:197/255.0 blue:23/255.0 alpha:1.0];
            
            line.frame = CGRectMake(1, height2, 320, 1);
            
            int offY = 0;
            if(count2 <5)
                offY = (100-count2*20)/2;
            
            for (int i = 0; i<count2; i++)
            {
                UILabel *seat = [[UILabel alloc] init];
                seat.font = NORMAL_FONT(font);
                seat.frame = CGRectMake(55, offY+i*20, length, 20);
                seat.backgroundColor = [UIColor clearColor];
                seat.text = [[[detailInfoDic objectForKey:@"seats"] objectAtIndex:i] objectForKey:@"description"];
                [cell.contentView addSubview:seat];
            }
        }
            break;
        case 2:
        {
            label.text = NSLocalizedString(@"position", @"");
            label.frame = CGRectMake(0, 0, 50, height3);
            label.backgroundColor = [UIColor colorWithRed:209/255.0 green:19/255.0 blue:117/255.0 alpha:1.0];
            
            line.frame = CGRectMake(1, height3, 320, 1);
            
            UILabel *address = [[UILabel alloc] init];
            address.text = [detailInfoDic objectForKey:@"address"];
            address.font = NORMAL_FONT(font);
            address.numberOfLines = 0;
            address.backgroundColor = [UIColor clearColor];
            address.frame = CGRectMake(55, 0, length, height3);
            [cell.contentView addSubview:address];
        }
            break;
        case 3:
        {
            label.text = NSLocalizedString(@"traffic", @"");
            label.frame = CGRectMake(0, 0, 50, height4);
            label.backgroundColor = [UIColor colorWithRed:24/255.0 green:161/255.0 blue:55/255.0 alpha:1.0];
            
            line.frame = CGRectMake(1, height4, 320, 1);
            
            UILabel *subway = [[UILabel alloc] init];
            NSString *subwayStr = [detailInfoDic objectForKey:@"subway"];
            subway.text = [NSString stringWithFormat:@"%@：%@",NSLocalizedString(@"metro", @""),subwayStr.length==0?NSLocalizedString(@"none", @""):subwayStr];
            subway.font = NORMAL_FONT(font);
            subway.numberOfLines = 2;
            subway.backgroundColor = [UIColor clearColor];
            subway.frame = CGRectMake(55, 10, length, 40);
            [cell.contentView addSubview:subway];
            
            UILabel *bus = [[UILabel alloc] init];
            NSString *busStr = [detailInfoDic objectForKey:@"bus"];
            bus.text = [NSString stringWithFormat:@"%@：%@",NSLocalizedString(@"bus", @""),busStr.length==0?NSLocalizedString(@"none", @""):busStr];
            bus.font = NORMAL_FONT(font);
            bus.numberOfLines = 2;
            bus.backgroundColor = [UIColor clearColor];
            bus.frame = CGRectMake(55, 50, length, 40);
            [cell.contentView addSubview:bus];
        }
            break;
        default:
            return nil;
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (IBAction)facilityDetailInfoAction:(UIButton *)sender
{
    if(sender.tag == 0)
    {
        FacilityLocation *instace = [[FacilityLocation alloc] init];
        instace.title = [detailInfoDic objectForKey:@"name"];
        instace.latitude = [[detailInfoDic objectForKey:@"latitude"] floatValue];
        instace.longitude = [[detailInfoDic objectForKey:@"longitude"] floatValue];
        NSLog(@"%f",instace.latitude);
        NSLog(@"%f",instace.longitude);
        [self.navigationController pushViewController:instace animated:YES];
    }
    else if(sender.tag == 1)
    {
        [[SendHttpOperation shareSendHttpRequest] getCompetitionList:detailInfoDic[@"nameEn"]];
        [SendHttpOperation shareSendHttpRequest].competitionListDelegate = self;
    }
    else
    {
        if([faclityCodeDic allKeys].count == 1)
            [self goto3D:faclityCodeDic[[faclityCodeDic allKeys][0]]];
        else
        {
            if(tempSelectArr)
            {
                tempSelectArr = nil;
            }
            
            tempSelectArr = [faclityCodeDic allKeys];
//            tempSelectArr = [tempSelectArr sortedArrayUsingSelector:@selector(compare:)];
            
            if(custom)
            {
                [custom removeFromSuperview];
                custom = nil;
            }
            custom = [[CustomSelect alloc] initWithFrame:INDICATOR_VIEW_FRAME title:NSLocalizedString(@"selectFaclity", @"") arr:tempSelectArr];
            custom.delegate = self;
            [self.view addSubview:custom];
        }
    }
}

- (void)selectDelegate:(int)buttonTag
{
    if(custom)
        [custom removeFromSuperview];
    
    NSLog(@"-->%d",buttonTag);
    
    if(0 <= buttonTag && buttonTag < 4)
        [self goto3D:faclityCodeDic[[faclityCodeDic allKeys][buttonTag]]];
}

- (void)goto3D:(NSString *)codeStr
{
    ShowVenuesViewController *ctl = [[ShowVenuesViewController alloc]init];
    NSLog(@"SceneNo-->%@",codeStr);
    ctl.SceneNo = codeStr;
    [self.navigationController pushViewController:ctl animated:YES];
}

- (void)passCompetitionList:(NSDictionary *)paramDic
{
    NSLog(@"passCompetitionList-->%@",paramDic);
    CompetitionList *instance = [[CompetitionList alloc] init];
    instance.competionArray = [paramDic objectForKey:@"retData"];
    [self.navigationController pushViewController:instance animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
